package com.xinyuan.trade.Adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xinyuan.trade.R;
import com.xinyuan.trade.Adaptive.UIAdapter;
import com.xinyuan.trade.Entity.TypeEntity;

public class TypeAdapter extends BaseAdapter {

	private Resources resources;
	private LayoutInflater layoutInflater;
	private List<TypeEntity> list;
	private UIAdapter uiadapter;

	// 构造方法，参数list传递的就是这一组数据的信息
	public TypeAdapter(Context context) {
		this.resources = context.getResources();
		layoutInflater = LayoutInflater.from(context);
		uiadapter = UIAdapter.getInstance(context);
		this.list = getTypeList();
	}

	// 得到总的数量
	public int getCount() {
		return this.list != null ? this.list.size() : 0;
	}

	// 根据ListView位置返回View
	public Object getItem(int position) {
		return this.list.get(position);
	}

	// 根据ListView位置得到List中的ID
	public long getItemId(int position) {
		return position;
	}

	// 根据位置得到View对象
	public View getView(int position, View convertView, ViewGroup parent) {
		TypeEntity entity = list.get(position);
		
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.item_type, null);
		}
		
		ImageView itemimage = (ImageView) convertView.findViewById(R.id.ItemImage);
		itemimage.setImageResource(entity.getType_icon());
		uiadapter.setMargin(itemimage, LayoutParams.MATCH_PARENT, 180, 0, 0, 0, 0);
		
		TextView itemtext = (TextView) convertView.findViewById(R.id.ItemText);
		itemtext.setText(resources.getString(entity.getType_name()));
		itemtext.setBackgroundResource(entity.getTv_bg());
		itemtext.setTextColor(resources.getColor(entity.getTv_color()));
		uiadapter.setTextSize(itemtext, 26);
		uiadapter.setMargin(itemtext, LayoutParams.MATCH_PARENT, 180, 0, 0, 0, 0);
		
		return convertView;
	}
	
	private List<TypeEntity> getTypeList() {
		List<TypeEntity> typelist = new ArrayList<TypeEntity>();
		
		// 包装
		TypeEntity type_packaging = new TypeEntity();
		type_packaging.setTv_color(R.color.white);
		type_packaging.setTv_bg(R.color.alpha_black);
		type_packaging.setType_name(R.string.type_packaging);
		type_packaging.setType_icon(R.drawable.img_type_packaging);
		typelist.add(type_packaging);
		
		// 家纺家装
		TypeEntity type_decoration = new TypeEntity();
		type_decoration.setTv_color(R.color.white);
		type_decoration.setTv_bg(R.color.alpha_black);
		type_decoration.setType_name(R.string.type_decoration);
		type_decoration.setType_icon(R.drawable.img_type_decoration);
		typelist.add(type_decoration);
		
		// 纺织
		TypeEntity type_textile = new TypeEntity();
		type_textile.setTv_color(R.color.white);
		type_textile.setTv_bg(R.color.alpha_black);
		type_textile.setType_name(R.string.type_textile);
		type_textile.setType_icon(R.drawable.img_type_textile);
		typelist.add(type_textile);
		
		// 服装内衣
		TypeEntity type_clothing_underwear = new TypeEntity();
		type_clothing_underwear.setTv_color(R.color.white);
		type_clothing_underwear.setTv_bg(R.color.alpha_red);
		type_clothing_underwear.setType_name(R.string.type_clothing_underwear);
		type_clothing_underwear.setType_icon(R.drawable.img_type_clothing_underwear);
		typelist.add(type_clothing_underwear);
		
		// 鞋包配饰
		TypeEntity type_accessories = new TypeEntity();
		type_accessories.setTv_color(R.color.black);
		type_accessories.setTv_bg(R.color.alpha_white);
		type_accessories.setType_name(R.string.type_accessories);
		type_accessories.setType_icon(R.drawable.img_type_accessories);
		typelist.add(type_accessories);
		
		// 美妆日化
		TypeEntity type_makeup = new TypeEntity();
		type_makeup.setTv_color(R.color.white);
		type_makeup.setTv_bg(R.color.alpha_black);
		type_makeup.setType_name(R.string.type_makeup);
		type_makeup.setType_icon(R.drawable.img_type_makeup);
		typelist.add(type_makeup);
		
		// 童装母婴
		TypeEntity type_maternal_infant = new TypeEntity();
		type_maternal_infant.setTv_color(R.color.white);
		type_maternal_infant.setTv_bg(R.color.alpha_black);
		type_maternal_infant.setType_name(R.string.type_maternal_infant);
		type_maternal_infant.setType_icon(R.drawable.img_type_maternal_infant);
		typelist.add(type_maternal_infant);

		// 日用百货
		TypeEntity type_daily_provisions = new TypeEntity();
		type_daily_provisions.setTv_color(R.color.red);
		type_daily_provisions.setTv_bg(R.color.alpha_white);
		type_daily_provisions.setType_name(R.string.type_daily_provisions);
		type_daily_provisions.setType_icon(R.drawable.img_type_daily_provisions);
		typelist.add(type_daily_provisions);

		// 电子电工
		TypeEntity type_electronic = new TypeEntity();
		type_electronic.setTv_color(R.color.white);
		type_electronic.setTv_bg(R.color.alpha_black);
		type_electronic.setType_name(R.string.type_electronic);
		type_electronic.setType_icon(R.drawable.img_type_electronic);
		typelist.add(type_electronic);
		
		// 数码家电
		TypeEntity type_digital_electrical_appliances = new TypeEntity();
		type_digital_electrical_appliances.setTv_color(R.color.white);
		type_digital_electrical_appliances.setTv_bg(R.color.alpha_black);
		type_digital_electrical_appliances.setType_name(R.string.type_digital_electrical_appliances);
		type_digital_electrical_appliances.setType_icon(R.drawable.img_type_digital_electrical_appliances);
		typelist.add(type_digital_electrical_appliances);
		
		// 机械五金
		TypeEntity type_mechanical_hardware = new TypeEntity();
		type_mechanical_hardware.setTv_color(R.color.white);
		type_mechanical_hardware.setTv_bg(R.color.alpha_black);
		type_mechanical_hardware.setType_name(R.string.type_mechanical_hardware);
		type_mechanical_hardware.setType_icon(R.drawable.img_type_mechanical_hardware);
		typelist.add(type_mechanical_hardware);
		
		// 冶金钢材
		TypeEntity type_steel = new TypeEntity();
		type_steel.setTv_color(R.color.white);
		type_steel.setTv_bg(R.color.alpha_red);
		type_steel.setType_name(R.string.type_steel);
		type_steel.setType_icon(R.drawable.img_type_steel);
		typelist.add(type_steel);
		
		// 化工精细
		TypeEntity type_chemical = new TypeEntity();
		type_chemical.setTv_color(R.color.red);
		type_chemical.setTv_bg(R.color.alpha_white);
		type_chemical.setType_name(R.string.type_chemical);
		type_chemical.setType_icon(R.drawable.img_type_chemical);
		typelist.add(type_chemical);

		// 橡胶塑料
		TypeEntity type_rubber_plastic = new TypeEntity();
		type_rubber_plastic.setTv_color(R.color.white);
		type_rubber_plastic.setTv_bg(R.color.alpha_black);
		type_rubber_plastic.setType_name(R.string.type_rubber_plastic);
		type_rubber_plastic.setType_icon(R.drawable.img_type_rubber_plastic);
		typelist.add(type_rubber_plastic);

		// 食品农业
		TypeEntity type_food_agriculture = new TypeEntity();
		type_food_agriculture.setTv_color(R.color.white);
		type_food_agriculture.setTv_bg(R.color.alpha_black);
		type_food_agriculture.setType_name(R.string.type_food_agriculture);
		type_food_agriculture.setType_icon(R.drawable.img_type_food_agriculture);
		typelist.add(type_food_agriculture);

		// 照明仪表安防
		TypeEntity type_lighting_instrument_security = new TypeEntity();
		type_lighting_instrument_security.setTv_color(R.color.black);
		type_lighting_instrument_security.setTv_bg(R.color.alpha_white);
		type_lighting_instrument_security.setType_name(R.string.type_lighting_instrument_security);
		type_lighting_instrument_security.setType_icon(R.drawable.img_type_lighting_instrument_security);
		typelist.add(type_lighting_instrument_security);
		return typelist;
	}
}
